package baidu.d20210907.t2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author habitplus
 * @since 2021-09-07 18:58
 */
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String[] ss = bf.readLine().split(" ");
        int t = Integer.parseInt(ss[0]);

        long k;
        for (int i = 0; i < t; i++) {
            k = Long.parseLong(bf.readLine());
            k = solve(k);
            System.out.println(k);
        }
        bf.close();
    }

    private static long solve(long k) {
        if (k < 1) return 0;
        char[] chs = String.valueOf(k).toCharArray();

        int i = 0;
        int n = chs.length;
        while (i < n) {
            if (chs[i] == '0') {
                if (i == 0) {
                    i++;
                    break;
                }
                chs[i] = '4';
                i--; //往前回溯
                chs[i]--;
            } else if (chs[i] > '3') {
                break;
            } else {
                i++;
            }
        }

        while (i < n) chs[i++] = '3';

        long ans = 0L;
        for (char ch : chs) {
            ans = ans * 10 + (ch - '0');
        }

        return ans;
    }
}
